package com.liuyucheng.idea_springboot.util;

import java.util.HashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

public class ReadByClassTask<T> implements Runnable {
    private Class<T> c;
    private int startSheet;
    private CountDownLatch countDownLatch;
    private String[] property;
    private Workbook wb;
    private int startRow;
    private int startColumn;
    private CopyOnWriteArrayList beanList;

    ReadByClassTask(Class c, int startSheet, CountDownLatch countDownLatch, String[] property, Workbook wb, int startRow, int startColumn, CopyOnWriteArrayList beanList) {
        this.c = c;
        this.startSheet = startSheet;
        this.countDownLatch = countDownLatch;
        this.property = property;
        this.wb = wb;
        this.startRow = startRow;
        this.startColumn = startColumn;
        this.beanList = beanList;
    }

    public void run() {
        try {
            HashMap<String, String> valueMap = new HashMap();
            Sheet sheet = this.wb.getSheetAt(this.startSheet);

            for(int rowNum = this.startRow; rowNum < sheet.getPhysicalNumberOfRows(); ++rowNum) {
                Row row = sheet.getRow(rowNum);
                int i = 0;

                for(int columnNum = this.startColumn; i < this.property.length; ++columnNum) {
                    Cell cell = row.getCell(columnNum);
                    valueMap.put(this.property[i], this.getCellValue(cell, cell.getCellType()));
                    ++i;
                }

                T t = this.c.newInstance();
                BeanUtils.populate(t, valueMap);
                this.beanList.add(t);
            }
        } catch (Exception var11) {
        } finally {
            this.countDownLatch.countDown();
        }

    }

    private String getCellValue(Cell rowCell, int rowCellType) {
        String value = "";
        switch(rowCellType) {
            case 1:
                value = rowCell.getStringCellValue();
                break;
            case 2:
                value = rowCell.getCellFormula();
            case 3:
            default:
                break;
            case 4:
                value = String.valueOf(rowCell.getBooleanCellValue());
                break;
            case 5:
                value = ErrorEval.getText(rowCell.getErrorCellValue());
        }

        return value;
    }
}
